﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class first : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();//调用数据源
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
       
            Response.Redirect("yggengxin.aspx");
        
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Response.Redirect("buy.aspx");
    }
    protected void BindData()
    {
        car tmp = new car();
        gvUser.DataSource = tmp.GetAllCar().Tables[0].DefaultView;//绑定数据源
        gvUser.DataBind();
    }

    protected void gvUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvUser.PageIndex = e.NewPageIndex;
        BindData();
    }

    
    
    
    

    
    protected void gvUser_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        car tmp = new car();
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowIndex == gvUser.EditIndex)
            {
                DataRowView rowItem = (DataRowView)e.Row.DataItem;
            }

            try
            {
                //获取“删除”链接
                LinkButton lnkbtnDelete = (LinkButton)e.Row.Cells[10].Controls[0];
                //添加JavaSeript代码实现客户端信息的提示
                lnkbtnDelete.OnClientClick = "return confirm('您真的要删除编号为" + e.Row.Cells[1].Text + "的记录吗？');";
            }
            catch
            {
                //若try块有异常，则不作任何操作
            }
        }
    }
    protected void gvUser_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvUser.EditIndex = e.NewEditIndex;
        BindData();
    }
    protected void gvUser_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvUser.EditIndex = -1;
        BindData();
    }
    protected void gvUser_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //取数据DataKeys数组
        //clees单元格  cells2第三列
        string num = gvUser.DataKeys[e.RowIndex].Value.ToString().Trim();
        string color = ((TextBox)(gvUser.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim();
        string namegys = ((TextBox)(gvUser.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim();
        string price = ((TextBox)(gvUser.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim();
        string dateout = ((TextBox)(gvUser.Rows[e.RowIndex].Cells[4].Controls[0])).Text.Trim();
        string datein = ((TextBox)(gvUser.Rows[e.RowIndex].Cells[5].Controls[0])).Text.Trim();
        string cknum = ((TextBox)(gvUser.Rows[e.RowIndex].Cells[6].Controls[0])).Text.Trim();
        string type = ((TextBox)(gvUser.Rows[e.RowIndex].Cells[7].Controls[0])).Text.Trim();
        string use = ((TextBox)(gvUser.Rows[e.RowIndex].Cells[8].Controls[0])).Text.Trim();
        string size = ((TextBox)(gvUser.Rows[e.RowIndex].Cells[9].Controls[0])).Text.Trim();
        string power = ((TextBox)(gvUser.Rows[e.RowIndex].Cells[10].Controls[0])).Text.Trim();
        car tmp = new car();
        tmp.num = num;
        tmp.color = color;
        tmp.namegys = namegys;
        tmp.price = price;
        tmp.dateout = dateout;
        tmp.datein = datein;
        tmp.cknum = cknum;
        tmp.type = type;
        tmp.use = use;
        tmp.size = size;
        tmp.power = power;
        tmp.Updateonecar(tmp);

        gvUser.EditIndex = -1;
        BindData();
    }
    protected void gvUser_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string num = gvUser.DataKeys[e.RowIndex].Value.ToString().Trim();

        car us = new car();
        us.num = num;
        us.Deleteonecar(us);
        BindData();
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        SQLHelper help = new SQLHelper();
        string type = this.DropDown1.SelectedValue.ToString();
        string newsKey = this.Tbkey.Text.Trim();
        string sqlStr = "select * from car where Ctype='" + type + "' and Ctype like '%" + newsKey + "%'";
        gvUser.DataSource = help.GetDataSet(sqlStr);
        gvUser.DataBind();
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        Response.Redirect("index.aspx");
    }
}